package com.video_study.util;

import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;


public class UpUtils {

	public static Integer upfile(MultipartFile file, HttpServletRequest request) {
		String realPath = request.getSession().getServletContext().getRealPath("/img/");
		//创建文件对象
		File file1 = new File(realPath);
		//判定当前文件是否存在，不存在则创建
		if (!file1.exists()) {
			file1.mkdirs();
		}
		//获取文件的名称   http......../video/liangzai.mp4
		String orgName = file.getOriginalFilename();
		FileOutputStream fos=null;
		try {
			fos = new FileOutputStream(realPath+orgName,true);
			fos.write(file.getBytes());
			fos.flush();
			return 1;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return 0;
		}finally {
			try {
				fos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

}
